package com.zxh.system.domain;

import org.springframework.data.domain.Page;

import java.util.Arrays;
import java.util.List;

public class JsonResult<T> {
    private String code;
    private String message;
    private boolean success=true;
    private List<T> data;
    private int PageCount;

    private static final JsonResult empty=new JsonResult(ResultCode.SUCCESS);

    public JsonResult(String code, String message, boolean success, List<T> data) {
        this.code = code;
        this.message = message;
        this.success = success;
        setData(data);
    }

    public JsonResult(String code,String message,boolean success){
        this(code,message,success,null);
    }

    public JsonResult(ResultCode resultCode, boolean success, List<T> data) {
        this(resultCode.code(),resultCode.msg(),success,data);
    }

    public JsonResult(ResultCode resultCode){
        this(resultCode,resultCode == ResultCode.SUCCESS);
    }

    public JsonResult(ResultCode resultCode,boolean success){
        this(resultCode,success,null);
    }

    public JsonResult(List<T> data) {
        this(ResultCode.SUCCESS,true,data);
    }

    public JsonResult(Page<T> page){
        this(page.getContent());
        setPageCount(page.getTotalPages());
    }

    public JsonResult(T t) {
        this(Arrays.asList(t));
    }

    public static JsonResult EMPTY(){
        return empty;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
        if(data!=null)
            setPageCount(data.size());
    }

    public int getPageCount() {
        return PageCount;
    }

    public void setPageCount(int pageCount) {
        PageCount = pageCount;
    }
}
